<?php

namespace App\Http\Controllers\Manage;

use App\Http\Controllers\Controller;
use App\Models\City;
use App\Models\Hospital;
use App\Models\Patient;
use App\Models\MedicalRecord;
use App\Models\UserBase;
use App\Models\SceneManage;
use Illuminate\Http\Request;

use Illuminate\Support\Facades\Auth;

class IndexController extends Controller
{
    protected $request;
    public $stHospitalModel;

    public function __construct(Request $request)
    {
        $this->request = $request;
        $this->stHospitalModel = new Hospital();
    }

    public function root()
    {
        return redirect(route('dashboard.index'));
    }

    public function index()
    {
        $user = $this->request->user();

        $type = ($user->hospital()->getQuery()->first(['type'])->type == 1) ? 'western' : 'chinese';

        return view('manage.index', ['user' => $user, 'type' => $type]);
    }

    public function search()
    {
        $user = $this->request->user();

        return view('manage.search', ['user' => $user]);
    }
    /* tds-页面 添加病例 */
    public function store()
    {
        $user = $this->request->user();
        // 1西医（默认），2中医，3中西医
        $hospitalType = $this->stHospitalModel->getTypeByHospitalId($user->hospital_id);
//        $u_type = $this->request->input('u_type',2);
        if ($user->isAdmin()) {// 平台管理员->所有有医生的医院下的所有医生  0
            if ($hospitalType === 1) {
                $hospitals = Hospital::hasDoctors()
                    ->where('isdel','=','0')
                    ->select(['id', 'name'])
                    ->where('type', $hospitalType)
                    ->get();
            } else {
                $hospitals = Hospital::hasDoctors()
                    ->where('isdel','=','0')
                    ->select(['id', 'name'])
                    ->whereIn('type', [2, 3])
                    ->get();
            }
            $doctors = $hospitals->first()->doctors(['id', 'name'])->getResults();

        } elseif ($user->isDoctorsAdmin()) {// 医院管理员-》一个医院下的所有医生  3
            $hospitals = $user->hospital()->getQuery()->select(['id', 'name'])->first();
            $doctors = $hospitals->doctors(['id', 'name'])->getResults();

        }elseif ($user->isRegionAdmin()){ // 区域管理员  4
            $hospitals_ids = SceneManage::where('account','=',$user->account)->select(['hospital_ids'])->first()->toArray();
            $hospitals_ids = json_decode($hospitals_ids['hospital_ids']);
            $hospitals_ids[] = $user->hospital_id;
            $hospitals = Hospital::whereIn('id',$hospitals_ids)->select(['id', 'name'])->get();
            $doctors = UserBase::where('hospital_id', $hospitals_ids[0])->whereIn('type',[2,3,4])->select(['id', 'name'])->get();

        } else {// (医生 )-》本人  2
            $hospitals = $user->hospital()->getQuery()->select(['id', 'name'])->get();
            $doctors = $user;
        }

        $city = new City();
        $address = $city->beforeLinkage();

        return view('manage.store', compact('hospitals', 'doctors', 'address', 'user'))
            ->with(['type' => ($user->hospital()->getQuery()->first(['type'])->type == 1) ? 'western' : 'chinese'])->with('selectid',$user->id);
    }

    public function subsequent(Request $request)
    {
        $user = \Auth::user();
        //yjq  添加复诊  start
        $type = $user->hospital->type;
        $patient = Patient::where('id',$request->input('patient_id'))->first();
        $hospital_info = MedicalRecord::where(
            [
                'patient_id'  => $patient->id,
                'visit_type'  => 1,//病例类型 1首诊  0复诊
                'hospital_id' => $patient->hospital_id,
                'doctor_id'   => $patient->doctor_id,
            ]
        )->orderBy('clinic_at','ASC')->first();
        $hospital_id = $patient->hospital_id;
        $doctors_id  = $patient->doctor_id;
        //获取医生的类型
        $intype = $this->doctorType();
        $doctors = UserBase::where('hospital_id', $hospital_id)
            ->whereIn('type', $intype)
            ->whereNull('deleted_at')
            ->select('id', 'name')
            ->orderBy('id', 'desc')
            ->get();
        $hospitals = Hospital::where([])->select(['id', 'name'])->get();
        //yjq 添加复诊  end(下面的代码是初始的逻辑)
//        $hospital_id = 0;
//        $doctors_id = 0;
//        if ($user->isAdmin()) {
//            $hospitals = Hospital::hasDoctors()->select(['id', 'name'])->get();
//
//            $doctors = $hospitals->first()->doctors(['id', 'name'])->getResults();
//        } elseif ($user->isDoctorsAdmin()) {
//
//            $hospitals = $user->hospital()->getQuery()->select(['id', 'name'])->first();
//            $doctors = $hospitals->doctors(['id', 'name'])->getResults();
//        } else {
//
//            $hospitals = $user->hospital()->getQuery()->select(['id', 'name'])->get();
//            $doctors = $user;
//        }

        return view('manage.subsequent', compact('hospitals', 'doctors', 'user','hospital_id','doctors_id'))
            ->with(['type' => ($user->hospital()->getQuery()->first(['type'])->type == 1) ? 'western' : 'chinese']);

    }

    /**
     * 获取医生的类型(哪些身份可以查到哪些类型的人)
     * @return array
     */
    public function doctorType()
    {
        $user = \Auth::user();
        $user_type = $user->type;
        $intype = [];
        switch ($user_type) {
            case 0://系统管理员
                $intype = ['0','2', '3', '4'];
                break;
            case 2://医生
                $intype = ['2'];
                break;
            case 3://医院管理员
                $intype = ['2', '3'];
                break;
            case 4://区域管理员
                $intype = ['2', '3', '4'];
                break;
        }

        return $intype;
    }
     //新添加⬇️
    public function test()
    {
        return view('manage.bloodsugar');
    }

}
